package elsa.controller;

import com.github.pagehelper.PageHelper;
import elsa.bean.ScsExp;
import elsa.bootmapper.ExpMapper;
import elsa.tools.Message;
import elsa.tools.MyResp;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.sql.Timestamp;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Controller
public class ExpController {

    @Autowired
    private ExpMapper expMapper;

    //查询经验列表兼 搜索 兼 筛选 兼 分页
    @CrossOrigin
    @RequestMapping(value = "/exp/all", method = RequestMethod.GET)
    @ResponseBody
    @RequiresPermissions("public")
    public MyResp expAllList(@RequestParam("pageIndex") Integer pageIndex,
                             @RequestParam("pageSize") Integer pageSize,
                             @RequestParam(value="value",required = false,defaultValue = "null") String value,
                             @RequestParam(value="type",required = false,defaultValue = "null") String type){
        if(pageIndex==null){
            pageIndex=1;
        }
        if(pageSize==null){
            pageSize=10;
        }
        PageHelper.startPage(pageIndex, pageSize);
        switch (type){
            case "Exptxr":
                return MyResp.render(0,"获取成功",expMapper.findAllExpByExptxr(value),expMapper.findExpCountByExptxr(value));
            case "Expname":
                return MyResp.render(0,"获取成功",expMapper.findAllExpByExpname(value),expMapper.findExpCountByExpname(value));
            case "":
                return MyResp.render(0,"获取成功",expMapper.findAllExp(),expMapper.findExpCount());
            default:
                return MyResp.render(0,"获取成功",expMapper.findAllExp(),expMapper.findExpCount());
        }
    }

    //查询经验信息
    @CrossOrigin
    @RequestMapping(value="/exp/info", method = RequestMethod.GET)
    @ResponseBody
    @RequiresPermissions("public")
    public ScsExp expInfo(@RequestParam("expnum") Integer expnum){
        if(expnum==null){
            expnum=-1;
        }
        return  expMapper.findExpInfo(expnum);
    }

    //新建经验
    @CrossOrigin
    @RequestMapping(value = "/exp/new", method = RequestMethod.POST)
    @ResponseBody
    @RequiresPermissions(value = {"surveyor","admin"},logical= Logical.OR)
    public Message addExp(@RequestBody ScsExp scsExp) {
        return new Message(setAddExp(scsExp));
    }

    //更新经验
    @CrossOrigin
    @RequestMapping(value = "/exp/update", method = RequestMethod.PUT)
    @ResponseBody
    @RequiresPermissions("admin")
    public Message updateExp(@RequestBody ScsExp scsExp){
        return new Message(setUpdateExp(scsExp));
    }

    //删除经验
    @CrossOrigin
    @RequestMapping(value = "/exp/delete", method = RequestMethod.DELETE)
    @ResponseBody
    @RequiresPermissions("admin")
    public Message deleteExp(@RequestParam("expnum") Integer expnum){
        return new Message(setDeleteExp(expnum));
    }

    //新建经验
    private String setAddExp(ScsExp scsExp){
        String b;
        if(scsExp.getExpname()==null||scsExp.getExpname().length()>20){
            b = "经验名称格式错误!";
        }else if (scsExp.getExptxr()==null||!chickName(scsExp.getExptxr())){
            b = "填写人格式错误!";
        }else {
            scsExp.setExptime(getNtime());
            expMapper.saveExp(scsExp);
            return "新建经验成功!";
        }
        return b;
    }

    //更新经验
    private String setUpdateExp(ScsExp scsExp){
        String b;
        if(scsExp.getExpnum()==null){
            scsExp.setExpnum(-1);
        }
        if(expMapper.findExpInfo(scsExp.getExpnum())==null){
            b = "经验不存在!";
        }else if(scsExp.getExpname()==null||scsExp.getExpname().length()>20){
            b = "经验名称格式错误!";
        }else if (scsExp.getExptxr()==null||!chickName(scsExp.getExptxr())){
            b = "填写人格式错误!";
        }else {
            scsExp.setExptime(getNtime());
            expMapper.updateExp(scsExp);
            b = "更新经验成功!";
        }
        return b;
    }

    //删除经验
    private String setDeleteExp(Integer expnum){
        String b;
        if(expnum==null||expMapper.findExpInfo(expnum)==null){
            b = "经验不存在!";
        }else {
            expMapper.deleteExp(expnum);
            b = "经验删除成功!";
        }
        return b;
    }

    //获取当前时间
    private Date getNtime(){
        Date date = new Date();
        return new Timestamp(date.getTime());
    }

    //检验姓名
    private boolean chickName(String name){
        Pattern pattern = Pattern.compile("(^[\u4e00-\u9fa5]{2,4}$)|(^[a-zA-Z]{3,7}$)");
        Matcher match=pattern.matcher(name);
        return match.matches();
    }
}
